﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_HKTransComp : DockContent
    {
        private DGBY.BLL.HKTransCompDataTab m_bllHKTransCompDataTab = new DGBY.BLL.HKTransCompDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectHKTransCompID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        public Frm_HKTransComp()
        {
            InitializeComponent();
        }

        private void Frm_HKTransComp_Load(object sender, EventArgs e)
        {
            InitDipQueryCondition();
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelete.Visible = true;
            }
        }

        private void InitDipQueryCondition()
        {
            dicQueryCondition.Add("香港运输公司名称", "HKTransCompName");
            dicQueryCondition.Add("公司地址", "HKTransCompAddr");
            dicQueryCondition.Add("公司电话", "HKTransCompTel");
            dicQueryCondition.Add("公司传真", "HKTransCompFax");
            dicQueryCondition.Add("经理姓名", "ManagerName");
            dicQueryCondition.Add("经理电话", "ManagerTel");
            dicQueryCondition.Add("联系人姓名", "HKTransConPeoName");
            dicQueryCondition.Add("联系人电话", "HKTransConPeoTel");
            dicQueryCondition.Add("开户行名称", "HKTransKHBank");
            dicQueryCondition.Add("开户行帐号", "HKTransCountNo");
            dicQueryCondition.Add("备注", "Note");
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            dataGridViewCompList.DataSource = null;
            m_ds = m_bllHKTransCompDataTab.GetList(strWhere);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompList.DataSource = m_ds.Tables[0];
                dataGridViewCompList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.HKTransCompDataTab modelHKTransCompDataTab = new DGBY.Model.HKTransCompDataTab();
                modelHKTransCompDataTab.HKTransCompName = this.textBoxCompName.Text;
                modelHKTransCompDataTab.HKTransCompTel = this.textBoxCompPhone.Text;
                modelHKTransCompDataTab.HKTransConPeoName = this.textBoxConPName.Text;
                modelHKTransCompDataTab.HKTransConPeoTel = this.textBoxConPPhone.Text;
                modelHKTransCompDataTab.HKTransCompAddr = this.textBoxAddress.Text;
                modelHKTransCompDataTab.HKTransKHBank = this.textBoxBank.Text;
                modelHKTransCompDataTab.HKTransCountNo = this.textBoxAccount.Text;
                modelHKTransCompDataTab.HKTransCompFax = this.textBoxFax.Text;
                modelHKTransCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelHKTransCompDataTab.ManagerTel = this.textBoxMngPhone.Text;
                modelHKTransCompDataTab.Note = this.richTextBoxNote.Text;


                int nResult = m_bllHKTransCompDataTab.Add(modelHKTransCompDataTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelHKTransCompDataTab.HKTransCompName);

                    m_nSelectHKTransCompID = m_bllHKTransCompDataTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView("");
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllHKTransCompDataTab.GetModel(m_nSelectHKTransCompID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.HKTransCompDataTab modelHKTransCompDataTab = m_bllHKTransCompDataTab.GetModel(m_nSelectHKTransCompID);
              //  modelHKTransCompDataTab.HKTransCompDataTabID = m_nSelectHKTransCompID;
                modelHKTransCompDataTab.HKTransCompName = this.textBoxCompName.Text;
                modelHKTransCompDataTab.HKTransCompTel = this.textBoxCompPhone.Text;
                modelHKTransCompDataTab.HKTransConPeoName = this.textBoxConPName.Text;
                modelHKTransCompDataTab.HKTransConPeoTel = this.textBoxConPPhone.Text;
                modelHKTransCompDataTab.HKTransCompAddr = this.textBoxAddress.Text;
                modelHKTransCompDataTab.HKTransKHBank = this.textBoxBank.Text;
                modelHKTransCompDataTab.HKTransCountNo = this.textBoxAccount.Text;
                modelHKTransCompDataTab.HKTransCompFax = this.textBoxFax.Text;
                modelHKTransCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelHKTransCompDataTab.ManagerTel = this.textBoxMngPhone.Text;
                modelHKTransCompDataTab.Note = this.richTextBoxNote.Text;

                m_bllHKTransCompDataTab.Update(modelHKTransCompDataTab);

                Frm_Main.Log2DB(this.Text, "修改", modelHKTransCompDataTab.HKTransCompName);
                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (m_bllHKTransCompDataTab.GetModel(m_nSelectHKTransCompID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllHKTransCompDataTab.Delete(m_nSelectHKTransCompID);
            ShowAllRecordInDataGridView("");

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        private void dataGridViewCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }
        private void dataGridViewCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strHKTransCompID = this.dataGridViewCompList.Rows[this.dataGridViewCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectHKTransCompID = Convert.ToInt32(strHKTransCompID, 10);
            cellclickHKTransComp();
        }

        private void cellclickHKTransComp()
        {
            DGBY.Model.HKTransCompDataTab modelHKTransCompDataTab = m_bllHKTransCompDataTab.GetModel(m_nSelectHKTransCompID);
            if (modelHKTransCompDataTab != null)
            {
                this.textBoxCompName.Text = modelHKTransCompDataTab.HKTransCompName;
                this.textBoxCompPhone.Text = modelHKTransCompDataTab.HKTransCompTel;
                this.textBoxConPName.Text = modelHKTransCompDataTab.HKTransConPeoName;
                this.textBoxConPPhone.Text = modelHKTransCompDataTab.HKTransConPeoTel;
                this.textBoxAddress.Text = modelHKTransCompDataTab.HKTransCompAddr;
                this.textBoxBank.Text = modelHKTransCompDataTab.HKTransKHBank;
                this.textBoxAccount.Text = modelHKTransCompDataTab.HKTransCountNo;
                this.textBoxFax.Text = modelHKTransCompDataTab.HKTransCompFax;
                this.textBoxManagerName.Text = modelHKTransCompDataTab.ManagerName;
                this.textBoxMngPhone.Text = modelHKTransCompDataTab.ManagerTel;
                this.richTextBoxNote.Text = modelHKTransCompDataTab.Note;
            }
            else
            {
                this.textBoxCompName.Text = "";
                this.textBoxCompPhone.Text = "";
                this.textBoxConPName.Text = "";
                this.textBoxConPPhone.Text = "";
                this.textBoxAddress.Text = "";
                this.textBoxBank.Text = "";
                this.textBoxAccount.Text = "";
                this.textBoxFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxMngPhone.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            string outFile = "";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                outFile = sfd.FileName;
            }
            else
            {
                return;
            }

            string templetFile = templetFilePath + "HKTransCompInfo.xlt";

            DataSet ds = m_bllHKTransCompDataTab.GetAllList();
            DataTable dt = ds.Tables[0];
            ExcelBase eB = new ExcelBase(templetFile, outFile);

            try
            {
                eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
            }
            catch (System.Exception ex)
            {

            }

            eB.Close();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();

            strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";


            //string strOrderBy = "\nORDER BY FactoryCode";
            //strWhere = strWhere + strOrderBy;
            DataSet ds = new DataSet();
            ds = m_bllHKTransCompDataTab.GetList(strWhere);

            dataGridViewCompList.DataSource = null;
            dataGridViewCompList.DataSource = ds.Tables[0];
            if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
            {
                this.dataGridViewCompList.DataSource = ds.Tables[0];
                this.dataGridViewCompList.Columns[0].Visible = false;
            }
            else
            {
                this.dataGridViewCompList.DataSource = null;
            }

            return;
        }


    }
}
